//
//  MyBranchDynamicViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/30.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MyBranchDynamicViewController.h"
#import "MyBranchDynamicjTableViewCell.h"
#import "MyBranchDynamicShowBigTableViewCell.h"
#import "LPDCDetailModel.h"
#import "ZJSqlit.h"
#import "LPWKHasCommentAndHeadLineViewController.h"
#import "STPhotoBroswer.h"
#define MOREIMAGE @"moreimage"
#define ONEIMAGE @"oneimage"
@interface MyBranchDynamicViewController ()<
UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

/**  分页  */
@property (assign, nonatomic) NSInteger  page;

/**  数据库  */
@property (strong, nonatomic) ZJSqlit  *newsSqlit;
/**  数据库表名  */
@property (strong, nonatomic) NSString  *sqName;
@end

@implementation MyBranchDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.titlelabel.text = @"支部动态";
       __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        NSString *dateString =  [NSDateFormatter stringTimeWithDate:[NSDate date]];
        [[NSUserDefaults standardUserDefaults] setObject:dateString forKey:@"START_TIME"];
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}
#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"START_INDEX"] = page;
    paramer[@"UNIT_ID"] = self.ID;
    paramer[@"PAGE_SIZE"] = @"10";
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getnewsactionListNewsGetAllNews] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ALLNEWS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataSouce.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"START_INDEX"] = page;
    paramer[@"UNIT_ID"] = self.ID;
    paramer[@"PAGE_SIZE"] = @"10";
    [NetWorkingManager postWithUrlString:[NetWorkingURL getnewsactionListNewsGetAllNews] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ALLNEWS"];
            
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}



- (void)action_leftButton:(UIButton *)sender{
    if (self.isMode) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    
}

#pragma mark - 协议代理
/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"2"]) {
        return 200 * [UIScreen zj_screenHScale];
    }else{
        return 155 * [UIScreen zj_screenHScale];
    }
    
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"2"]) {
        MyBranchDynamicShowBigTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MOREIMAGE forIndexPath:indexPath];
        cell.model = model;
        return cell;

    }else{
        MyBranchDynamicjTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ONEIMAGE forIndexPath:indexPath];
        cell.model = model;
        return cell;

    }

    
    
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"2"]) {
        if (model.IMAGEJSON.count != 0) {
            NSMutableArray *images = [NSMutableArray array];
            for ( NSDictionary *dic in model.IMAGEJSON) {
                NSString *imagestring = [dic objectForKey:@"imageUrl"];
                [images addObject:imagestring];
            }
            STPhotoBroswer *brower = [[STPhotoBroswer alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) ImageArray:images currentIndex:0];
            [brower show];
            
        }

    }else{
        LPWKHasCommentAndHeadLineViewController *vc = [LPWKHasCommentAndHeadLineViewController new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[MyBranchDynamicjTableViewCell class] forCellReuseIdentifier:ONEIMAGE];
        [_tableView registerClass:[MyBranchDynamicShowBigTableViewCell class] forCellReuseIdentifier:MOREIMAGE];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (ZJSqlit *)newsSqlit{
    if (!_newsSqlit) {
        _newsSqlit = [ZJSqlit new];
    }
    return _newsSqlit;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
